package gentle.service.impl;

import gentle.base.SqlMapper;
import gentle.base.StringUtil;
import gentle.entity.CommUtils;
import gentle.entity.CustomerInfo;
import gentle.exception.DateTimeKit;
import gentle.service.ICustomerPublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;


@Service
@Transactional
public class CustomerPublicServiceImpl implements ICustomerPublicService {

    @Autowired
    private SqlMapper sqlMapper;

    //得到黄页编号
    public String getMaxYellowNumber(String abbreviation) {
        String ctable=abbreviation+"customer_info";
        String sql2 = " select id,max(yellow_number) as yellowNumber from " + ctable + " limit 1;";
        CustomerInfo customerInfo = sqlMapper.selectOne(sql2, CustomerInfo.class);
        //设置黄页编号     //设置黄页编号
        String yellowNumbers = "0000123";
        return yellowNumbers + "";
    }

    //获取生成主键id
    public String getId(String abbreviation) {
        String id = CommUtils.getUUID();
        id = id + abbreviation;
        Random rand = new Random();
        int randomNumber = rand.nextInt(999999);
        id = id + randomNumber;
        return id;
    }

    //设置开始时间
    public String getStartTime() {
        String starttime = DateTimeKit.now();
        String time = starttime.substring(0, 11);
        int hour = Integer.parseInt(starttime.substring(11, 13));
        int minute = Integer.parseInt(starttime.substring(14, 16));
        minute = (minute / 15) * 15;
        String minutes = minute + "";
        if ("0".equals(minutes)) {
            minutes = "00";
        }
        starttime = time + hour + ":" + minutes + ":00";
        return starttime;
    }

    //设置结束时间
    public String getEndTime() {
        String starttime = DateTimeKit.now();
        String time = starttime.substring(0, 11);
        int hour = Integer.parseInt(starttime.substring(11, 13));
        int minute = Integer.parseInt(starttime.substring(14, 16));
        minute = (minute / 15) * 15;
        String minutes = minute + "";
        if ("0".equals(minutes)) {
            minutes = "00";
        }
        String endtime = "";
        if (hour + 1 == 24) {
            time = time.substring(0, 9) + (Integer.parseInt(time.substring(9, 10)) + 1) + " ";
            endtime = time + "00" + ":" + minutes + ":00";
        } else {
            endtime = time + (hour + 1) + ":" + minutes + ":00";
        }
        return endtime;
    }

    //患者第二次到店，判断是今天到店的还是以前来过，今天到店多次客户状态都是新客首次，
    public String getNewCustomerStatus(String reservationsId,String abbreviation) {
        String sql = "select id from distinction_customer_status where " +
                " reservations_id='" + reservationsId + "' and left(add_time,10)<left(now(),10) and status=1 ; ";
        List<CustomerInfo> list = sqlMapper.selectList(sql, CustomerInfo.class);
        if (list.size() > 0) {
            return getOldOrReconsultingStatus(reservationsId,abbreviation);
        } else {
            return "新客首次";
        }
    }

    //点重咨，判断之前来店是否有成交，有为老客，从来没有为新客多次
    public String getOldOrReconsultingStatus(String reservationsId,String abbreviation) {
        String ctable = abbreviation + "customer_info";
        String sql = "select id from distinction_customer_status where " +
                " reservations_id='" + reservationsId + "' and deal_status='已成交' and left(update_time,10)<left(now(),10) and status=1 ; ";
        List<CustomerInfo> list = sqlMapper.selectList(sql, CustomerInfo.class);
        if (list.size() > 0) {
            return "老客";
        } else {
            String sql1 = "select id from "+ctable+" where " +
                    " reservations_id='" + reservationsId + "' and customer_status='新客多次' and left(arrival_time,10)<left(now(),10) ";
            List<CustomerInfo> list1 = sqlMapper.selectList(sql1, CustomerInfo.class);
            if(list1.size()>0){
                return "新客N次";
            }else{
                return "新客多次";
            }
        }
    }

    //年龄获得头像
    public String getSexPt(CustomerInfo info) {
        Integer customerAge=info.getCustomerAge();
        Integer sex=info.getCustomerSex();
        if (StringUtil.isEmpty(customerAge)) {
            return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png";
        }else if (customerAge >= 0 && customerAge < 18) {
            if (sex==1) {
                return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/nan-child.png";
            } else if (sex==2) {
                return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/nv-child.png";
            } else {
                return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png";
            }
        } else if (customerAge  >= 18 && customerAge  <= 50) {
            if (sex==1) {
                return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/female-adult.png";
            } else if (sex==2) {
                return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/lfemale-child.png";
            } else {
                return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png";
            }
        } else if (customerAge  > 50) {
            if (sex==1) {
                return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/male-old.png";
            } else if (sex==2) {
                return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/female-old.png";
            } else {
                return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png";
            }
        }else{
            return "https://pynnn.oss-cn-hangzhou.aliyuncs.com/unkown-sex.png";
        }
    }

    //点亮获取病历号
    public String getMedicalRecordNumbers(String customerPhone, String abbreviation) {
        String ctable = abbreviation + "customer_info";
        String upsql = "update " + abbreviation + " set customer_phone=null where customer_phone='" + customerPhone + "' ";
        String usql = "update " + abbreviation + " set customer_phone='" + customerPhone + "'" +
                " where customer_phone is null order by id asc limit 1 ";
        sqlMapper.update(upsql);
        sqlMapper.update(usql);
        String ssql2 = "select concat(prefix_part_one,prefix_part_two,num) as medicalRecordNumber from " + abbreviation + " where customer_phone='" + customerPhone + "' limit 1";
        CustomerInfo cinfo2 = sqlMapper.selectOne(ssql2, CustomerInfo.class);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("tableName", ctable);
        params.put("medicalRecordNumber", cinfo2.getMedicalRecordNumber());
        return cinfo2.getMedicalRecordNumber();
    }

    //首次到店的患者信息
    public CustomerInfo firstVisitInformations(String reservationsId) {
        String abbreviationSql = "select abbreviation from distinction_customer_status where reservations_id = '" + reservationsId + "' ";
        List<String> abbreviationList = sqlMapper.selectList(abbreviationSql, String.class);
        String customerTablesd = "";
        for (int i = 0; i < abbreviationList.size(); i++) {
            String head = abbreviationList.get(i);
            if (i == abbreviationList.size() - 1) {
                customerTablesd += "select * from " + head + "customer_info t where reservations_id='" + reservationsId + "' and arrival_time is not null";
            } else {
                customerTablesd += "select * " +
                        "from " + head + "customer_info t where reservations_id='" + reservationsId + "' " +
                        "and arrival_time is not null  union ";
            }
        }
        if (abbreviationList.size() == 0) {
            return null;
        }
        CustomerInfo customerInfo = this.selectOneByReservationsIdFirsts(reservationsId, customerTablesd);  //首次到店患者信息
        return customerInfo;
    }

    //患者初次到店信息，如获得初诊tc
    public CustomerInfo selectOneByReservationsIdFirsts(String reservationsId, String customerTablesd) {
        String sql = "select * from (" + customerTablesd + ")  t order by t.arrival_time asc limit 1;";
        CustomerInfo customerInfo = sqlMapper.selectOne(sql, CustomerInfo.class);
        return customerInfo;
    }

}